package com.smalldragon.yml.config;

import cn.dev33.satoken.stp.StpInterface;
import com.smalldragon.yml.context.dto.LoginDTO;
import com.smalldragon.yml.api.CommonApiService;
import com.smalldragon.yml.context.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author YML
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private CommonApiService commonApiService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        LoginDTO loginUser = UserContext.getLoginUser();
        // 如果是管理员则免权限
        if (loginUser.getRoles().contains("admin")){
            return Collections.singletonList("*");
        }else {
            return commonApiService.getFunctionDataByRoles(loginUser.getRoles());
        }
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        LoginDTO loginUser = UserContext.getLoginUser();
        // 如果是管理员则免权限
        if (loginUser.getRoles().contains("admin")){
            return Collections.singletonList("*");
        }else {
            String[] split = loginUser.getRoles().split(",");
            return Arrays.asList(split);
        }

    }

}
